// 导入用户集合
const { Message } = require('../../../model/message');
const { decodeToken } = require('../../../middleGuard/jwt')

module.exports = async (req, res) => {
  const id = decodeToken(req.headers.authorization).userId
  const values = '_id avatar nickname'
  const list = await Message.find({ toUser: id }).populate('fromUser', values).populate('toUser', values)
  const list2 = await Message.find({ fromUser: id }).populate('toUser', values).populate('fromUser', values)

  let sortList = [...list, ...list2]
  sortList = sortList.sort((next, pre) => pre.createTime - next.createTime )
  res.json({
      data: sortList
  })
}